#pragma once

#include <string.h>

namespace host {

    typedef struct
    {
        struct {
            unsigned int u32ChunkID;
            unsigned int u32ChunkSize;
            unsigned int u32Format;
        }ChunkHeader_t;

        struct {
            unsigned int   u32SubChunkID;
            unsigned int   u32SubChunkSize;
            unsigned short u16AudioFormat;
            unsigned short u16NumChannels;
            unsigned int   u32SampleRate;
            unsigned int   u32ByteRate;
            unsigned short u16BlockAlign;
            unsigned short u16BitsPerSample;
        }FmtChunk_t;

        struct {
            unsigned int u32SubChunkID;
            unsigned int u32SubChunkSize;
        }DataChunk_t;
    }Wave_t;

    class WavFile {
    public:
        WavFile();
        virtual ~WavFile() {};

        bool Open(const char* i_pPath);
        bool Create(const char* i_pPath, unsigned int i_u32SampleRate = 48000, unsigned int i_u32Channel = 2, unsigned int i_u32BitsPerSample = 16);
        void Close();

        int  Read(unsigned char* o_pBuffer, unsigned int i_u32Len);
        int  Write(unsigned char* i_pBuffer, unsigned int i_u32Len);
        bool Seek(unsigned int i_u32Pos);
        bool Refresh();

        unsigned int GetSampleRate() { return m_WaveHdr.FmtChunk_t.u32SampleRate; };
        unsigned int GetChannel() { return m_WaveHdr.FmtChunk_t.u16NumChannels; };
        unsigned int GetBitsPerSample() { return m_WaveHdr.FmtChunk_t.u16BitsPerSample; };
        unsigned int GetDataLength() { return m_u32DataLen; };

    private:
        FILE* m_pFile;
        Wave_t       m_WaveHdr;
        unsigned int m_u32DataLen;
        unsigned int m_u32DataPos;
    };
};
